using Nebukam.Common;
using Nebukam.Common.Editor;
using Nebukam.ORCA;
using EnumDefine;
using QuadTree;
using SG;
using System.Collections.Generic;
using TMPro;
using Unity.Collections;
using UnityEngine;

public class LogicGame : LogicBase
{
    static LogicGame m_Instance;

    ORCABundle<Agent> m_Simulation;
    AgentGroup<Agent> m_Agents = new AgentGroup<Agent>();
    RaycastGroup m_Raycasts = new RaycastGroup();
    ObstacleGroup m_Dynamics = new ObstacleGroup();

    public int m_AgentNum;
    public static LogicGame Instance()
    {
        if( m_Instance == null)
            m_Instance = InstanceHelper.Singleton<LogicGame>();
        return m_Instance;
    }

    
    // 申请这个角色得移动节点
    public Agent DoRegistAgent()
    {
        return m_Simulation.NewAgent(Vector3.one * 1000);

    }
    // 删掉这个角色得移动节点
    public void DoUnregistAgent(Agent agent)
    {
        m_Agents.Remove(agent);
    }



    private void Awake()
    {
        m_Simulation = new ORCABundle<Agent>();
        m_Simulation.plane = AxisPair.XY;
    }
    void Start()
    {
        m_Instance = this;
    }
    public int m_HumenNum = 10;
    public float m_fRadius = 4;
    public GameObject m_HumenProfab;
    public List<ObjectHumen> m_EnemyList = new List<ObjectHumen>();
    public List<ObjectHumen> m_PlayerList = new List<ObjectHumen>();

    //QuadTreeSystem m_EnemyTree;
    //QuadTreeSystem m_PlayerTree;
    //战斗开始
    public void DoStart()
    {
        for (int i = 0; i < m_HumenNum; ++i)
        {
            var obj = ObjectMgr.Instance().CreateObj(m_HumenProfab, ObjectMgr.Instance().GetObjectRoot());
            var humen = obj.GetComponent<ObjectHumen>();
            humen.DoInit(i);
            humen.DoEnterGame( Map.Instance().GetEnemyHome(), 0);
            m_EnemyList.Add(humen);
        }
    }
    // Update is called once per frame
    public int m_nCheckNum = 1;

    public ObjectHumen GetEnmey(int nIndex)
    {
        return m_EnemyList[nIndex];
    }

    public override void DoUpdate()
    {
        base.DoUpdate();
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //SetTowerToBlock(TowerType.Single, m_CurSelect);
        }
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            //m_CurSelect.DoFlooring();
        }
    }
    void Update()
    {
        m_Simulation.orca.Schedule(Time.deltaTime);
        if (Input.GetKeyDown(KeyCode.F))
            DoStart();
        Draw.Circle2D(Vector3.zero, m_fRadius, Color.yellow, 50);
    }
    public bool bUseQuad = false;
    private void OnApplicationQuit()
    {
        m_Simulation.Dispose();
    }
    //private void LateUpdate()
    //{
    //    if (m_Simulation.orca.TryComplete())
    //    {
    //        //var job = new JobQuadTreeFull()
    //        //{
    //        //    m_Tree = m_EnemyTree,
    //        //    m_Type = 0,
    //        //};
    //        //var handle = job.Schedule();
    //        //handle.Complete();
    //        //if (bUseQuad)
    //        //{
    //        //    m_EnemyTree.ClearData();
    //        //    for (int i = 0; i < m_EnemyList.Count; ++i)
    //        //        m_EnemyTree.AddCore(m_EnemyList[i]);
    //        //    m_EnemyTree.DrawDebug();
    //        //}
    //        //m_PlayerTree.ClearData();
    //        //for( int i = 0; i < m_PlayerList.Count; ++ i )
    //        //    m_PlayerTree.AddCore(m_PlayerList[i]);
    //    }
    //    m_AgentNum = m_Simulation.agents.Count;
    //}

    public ObjectScene GetTarget(Vector2 pos, Team team)
    {
        var list = m_EnemyList;
        if (team == Team.Player)
            list = m_PlayerList;
        float minDistance = float.MaxValue;
        ObjectScene nearestObject = null;
        foreach (var obj in list )
        {
            float distance = Vector3.Distance( pos, obj.GetPositon());
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestObject = obj;
            }
        }
        return nearestObject;
    }

    public ObjectScene CheckCollider( Vector2 start, Vector2 pos, float fRadius, Team team)
    {

        return null;
    }
}
